import 'dart:typed_data';
import 'package:bluetooth_parse_plugin/src/base/data_base.dart';
import 'package:bluetooth_parse_plugin/src/lorin/lorin_enum.dart';

// Lorin帧包中的数据段
class LorinData extends DataBase {
  // 数据类型（标准组/专用组）
  LorinDataFormatType dataFormatType;

  // 源设备App
  int? sourceDeviceNum = LorinSourceDeviceType.app.rawValue;

  // 目标设备
  int? targetDeviceNum;

  // 通道(标准组)
  LorinDataChannelType? channelType;

  // 属性(标准组)
  LorinDataPropertyType? propertyType;

  // cmd
  Uint8List? cmd;

  // 参数
  Uint8List? para;

  LorinData({
    required this.dataFormatType,
    required this.sourceDeviceNum,
    required this.targetDeviceNum,
    required this.channelType,
    required this.propertyType,
    required this.cmd,
    required this.para,
  });

  @override
  int dataIntValue() {
    var s = sourceDeviceNum ?? 0;
    var t = targetDeviceNum ?? 0;
    var c = channelType?.rawValue ?? 0;
    var p = propertyType?.rawValue ?? 0;
    var cmd_ = computeDataIntVal(cmd ?? Uint8List.fromList([0]));
    var para_ = para == null ? 0 : computeDataIntVal(para!);
    switch (dataFormatType) {
      case LorinDataFormatType.norCpp:
        return c + p + para_;
      case LorinDataFormatType.norTcpp:
        return t + c + p + para_;
      case LorinDataFormatType.norStcpp:
        return s + t + c + p + para_;
      case LorinDataFormatType.speCp:
        return cmd_ + p;
      case LorinDataFormatType.speTcp:
        return t + cmd_ + p;

      case LorinDataFormatType.speStcp:
        return s + t + cmd_ + p;
      default:
        return 0;
    }
  }

  @override
  int dataLength() {
    var paraLength = para?.length ?? 0;
    int cmdLength = cmd?.length ?? 0;
    switch (dataFormatType) {
      case LorinDataFormatType.norCpp:
        return paraLength + 2;
      case LorinDataFormatType.norTcpp:
        return paraLength + 3;
      case LorinDataFormatType.norStcpp:
        return paraLength + 4;
      case LorinDataFormatType.speCp:
        return paraLength + cmdLength;
      case LorinDataFormatType.speTcp:
        return paraLength + cmdLength + 1;
      case LorinDataFormatType.speStcp:
        return paraLength + cmdLength + 2;
      default:
        return 0;
    }
  }

  @override
  Uint8List toBytes() {
    List<int> dataList = [];
    var s = sourceDeviceNum ?? 0;
    var t = targetDeviceNum ?? 0;
    var c = channelType?.rawValue ?? 0;
    var p = propertyType?.rawValue ?? 0;
    var cmd_ = cmd ?? Uint8List.fromList([0]);
    switch (dataFormatType) {
      case LorinDataFormatType.norCpp:
        dataList.addAll([c, p]);
        break;
      case LorinDataFormatType.norTcpp:
        dataList.addAll([t, c, p]);
        break;
      case LorinDataFormatType.norStcpp:
        var isReq = LorinSourceDeviceType.app.rawValue == sourceDeviceNum;
        // 0x80 bit7为1代表响应数据
        dataList.addAll([
          s,
          t,
          c + (isReq ? 0x00 : 0x80),
          p,
        ]);
        break;
      case LorinDataFormatType.speCp:
        dataList.addAll(cmd_);
        break;
      case LorinDataFormatType.speTcp:
        dataList.add(t);
        dataList.addAll(cmd_);
        break;
      case LorinDataFormatType.speStcp:
        dataList.addAll([s, t]);
        dataList.addAll(cmd_);
        break;
      default:
        break;
    }
    // 添加参数
    if (para != null) dataList.addAll(para!);
    return Uint8List.fromList(dataList);
  }
}
